package com.promis.crypto.digest;

import com.promis.crypto.BASE64Coder;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class BaseDigest {

	protected String digestAlgorithm;
	
	public BaseDigest(String digestAlgorithm) {
		this.digestAlgorithm = digestAlgorithm;
	}
	
	public byte[] digest(byte[] data) throws NoSuchAlgorithmException {
		MessageDigest md = MessageDigest.getInstance(digestAlgorithm);
		md.update(data);
		return md.digest();
	}
	
	public static String toKey(byte[] key) {
		return BASE64Coder.encryptBASE64(key);
	}
	
	public static byte[] toKey(String key) throws IOException {
		return BASE64Coder.decryptBASE64(key);
	}
}
